package com.station.server.entity;

import com.station.client.proxies.PFuelTank;

import javax.persistence.*;
import java.util.List;

/**
 * User: JinJavaMojo
 * Date: 25.09.13
 */
@Table(name = "FUEL_TANK", schema = "public", catalog = "")
@Entity
public class FuelTank extends PlaceableEntity {

    @Column(name = "MAX_CAPACITY")
    private double maxCapacity;

    @Column(name = "CURRENT_CAPACITY")
    private double currentCapacity;

    @Column(name = "FUEL")
    private String fuel;

    @OneToMany(cascade=CascadeType.ALL, targetEntity = PetrolPump.class)
    private List<PetrolPump> pumps;

    @JoinColumn(name="STATION_ID")
    @ManyToOne(optional = true, cascade = CascadeType.ALL,  targetEntity = Station.class)
    private Station stationId;

    public double getCurrentCapacity() {
        return currentCapacity;
    }

    public void setCurrentCapacity(double currentCapacity) {
        this.currentCapacity = currentCapacity;
    }

    public double getMaxCapacity() {
        return maxCapacity;
    }

    public void setMaxCapacity(double maxCapacity) {
        this.maxCapacity = maxCapacity;
    }

    public Station getStationId() {
        return stationId;
    }

    public void setStationId(Station stationOwner) {
        this.stationId = stationOwner;
    }

    public List<PetrolPump> getPumps() {
        return pumps;
    }

    public void setPumps(List<PetrolPump> pumps) {
        this.pumps = pumps;
    }

    public Fuel getFuel() {
        return fuel!= null ? Fuel.getInstance(fuel) : null;
    }

    public void setFuel(Fuel fuel) {
        this.fuel = fuel != null ? fuel.getType() : null;
    }

    public static FuelTank transform(PFuelTank pTank) {
        FuelTank entity = (FuelTank) BaseEntity.getInstance(pTank, FuelTank.class);
        entity.size = Size.transform(pTank.pSize);
        entity.coordinates = Coordinates.transform(pTank.pCoordinates);
        entity.setCurrentCapacity(pTank.getCurrentCapacity());
        entity.setMaxCapacity(pTank.getMaxCapacity());
        entity.persist();
        //todo foreach pertrolpumps transform
        return entity;
    }
}
